﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Serviio.API;

namespace Serviio.WHS
{
    public partial class ChangeServiceAccountForm : Form
    {
        private static readonly LogFacade log = LogFacade.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public ChangeServiceAccountForm()
        {
            InitializeComponent();
        }

        private ServiioConnection conn = null;

        public bool ChangeServiceLogin(ServiioConnection connection)
        {
            try
            {
                conn = connection;
                if (this.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    return true;
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            return false;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            try
            {
                string username = textBoxUsername.Text,
                       password = textBoxPassword.Text;

                bool valid = true;

                if (radioLocalSystem.Checked)
                {
                    username = "";
                    password = "";
                }
                else
                {
                    valid = valid && !String.IsNullOrEmpty(username);
                    valid = valid && password.Equals(textBoxPassword2.Text);
                }

                if (!valid)
                {
                    MessageBox.Show("Either the username is invalid or the passwords do not match. Please try again.", "Service Change Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (conn.ChangeServiioServiceAccount(username, password))
                    {
                        conn.StopServiioService();
                        if (conn.StartServiioService())
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                            return;
                        }
                    }
                    MessageBox.Show("An error was encountered trying to change the service login account.", "Service Change Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            this.Enabled = true;
        }

        private void radioOther_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (radioOther.Checked)
                {
                    panelCredentials.Enabled = true;
                    textBoxUsername.SelectAll();
                    textBoxUsername.Focus();
                }
                else
                {
                    panelCredentials.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
    }
}
